package group2projman;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class Transaction implements DbWritable {
	public enum Type {
		LOAN, RENEW, RETURN, PAYMENT, DAMAGE_CHARGE
	}
	
	int id;	
	
	Date startDate;
	
	private Type type;
	
	public Transaction(int id, Type type) {
		this.id = id;
		this.type = type;
		
		startDate = new Date();
	}
	
	public Transaction.Type getType() {
		return type;
	}
	
	public int getId() {
		return id;
	}
	
	/**
	 * Concrete class string will consist of 8 attributes:
	 * id, startDate, type, bookCopyId, dueDate, lateFee, amount, description
	 */
	public String listAttributes() {
		StringBuffer result = new StringBuffer();
		
		result.append("int:").append("id;").append(id);		
		result.append(",dat:").append("startDate;").append(startDate);
		result.append(",str:").append("type;").append(type);
		
		return result.toString();
	}
	
	public boolean loadAttributes(String desc) {
		try {
			String[] split = desc.split("[;,]");	
			
			if (!split[0].equals("int:id"))
				return true;	
			id = Integer.parseInt(split[1]);
			
			if (!split[2].equals("dat:startDate"))
				return true;	
			startDate = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(split[3]);
			
			if (!split[4].equals("str:type"))
				return true;	
			type = Type.valueOf(split[5]);					
		}  catch (Exception e) {
			// Array index out of bounds or enum value not found
			return true;
		}
		
		return false;
	}
}
